;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.AgentInventoryReport", [])
        .controller("AgentInventoryReportCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, $stateParams) {
            // tab标签展示布尔值
            $scope.activeFlag = [true, false, false];
            var pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 分页信息
            $scope.pageInfo = angular.copy(pageInfo);
            // 导出功能信息
            $scope.exportParam = {
                url: ybHttp.RequestUrl("device", "/report/exportAgentInventoryAmount"),
            };
            // 搜索条件
            $scope.condition = {};
            // 数据列表
            $scope.dataList = [];
            // 总计的数据
            $scope.totalRow = [];
            // 高级搜索和配置表格项的展示与隐藏
            $scope.toolbarConfig = {
                showAgentColConfig: false,
                showAgentAdSearch: false,
            };
            // 公共表格头信息
            $scope.tbConf = {
                scrollWidth: 160,
                searchTableCol: [],
                tableCol: [],
                tableTopHead: [{name: '库存金额（单位：万）', colSpan: 1, width: 120}]
            };

            $scope.selectClassInfo = {
                notCheckedClassIds: [],
                selectClassesStr: ''
            };

            // 重置搜索条件
            $scope.reset = function () {
                $scope.condition = {};
                $scope.selectClassInfo = {
                    notCheckedClassIds: [],
                    selectClassesStr: ''
                };
                // 恢复顶部表格头原有宽度和colspan
                setTopHead($scope.tbConf.tableCol.length + 1);
                // 恢复原有表格头的展示
                angular.forEach($scope.tbConf.tableCol, function (item) {
                    item.isShow = true;
                });
                // 恢复分类搜索的已选状态
                angular.forEach($scope.tbConf.searchTableCol, function (item) {
                    item.isChecked = true;
                });
            };

            // 搜索供货关系列表
            $scope.search = function (curPage) {
                getDataList(curPage);
            };

            // 切换高级搜索和表格配置展示与隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig[toolbar]) {
                    $scope.toolbarConfig[toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig[toolbar] = true;
                    });
                }
            };

            // 高级搜索中的选择分类
            $scope.selectClass = function () {
                var checkedClass = [];          // 已选中的分类
                var notCheckedClassIds = [];
                angular.forEach($scope.tbConf.searchTableCol, function (item) {
                    if (item.isChecked) {
                        checkedClass.push(item.name);
                    } else {
                        notCheckedClassIds.push(item.id);
                    }
                });
                $scope.selectClassInfo.selectClassesStr = checkedClass.join(',');
                $scope.selectClassInfo.notCheckedClassIds = notCheckedClassIds;
            };

            // 高级搜索中的搜索
            $scope.confirmSearch = function () {
                var classNameList = [];
                var checkLength = 1;
                angular.forEach($scope.tbConf.tableCol, function (item) {
                    if ($scope.selectClassInfo.notCheckedClassIds.indexOf(item.id) >= 0) {
                        item.isShow = false;
                        classNameList.push(item.name);
                    } else {
                        item.isShow = true;
                        checkLength++;
                    }
                });
                setTopHead(checkLength);
                $scope.condition.classNameList = classNameList;
                $scope.search(1);
            };

            // 设置表格头最顶部的宽度和colspan
            function setTopHead (num) {
                $scope.tbConf.tableTopHead[0].colSpan = num;
                $scope.tbConf.tableTopHead[0].width = num*120;
            }

            // 获取动态表格头
            function getTableHead () {
                var url = '/materialClass/getAllSecondlist';
                ybHttp.Get('device', url).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    var tableCol = [];
                    var searchTableCol = [];
                    angular.forEach(res.value, function (item) {
                        tableCol.push({
                            name: item.materialCalssName,
                            width: 120,
                            isShow: true,
                            id: item.id
                        });
                        searchTableCol.push({
                            name: item.materialCalssName,
                            isChecked: true,
                            id: item.id,
                            value: item.materialCalssName
                        })
                    });
                    setTopHead(tableCol.length + 1);
                    $scope.tbConf.searchTableCol = searchTableCol;
                    $scope.tbConf.scrollWidth = 300 + tableCol.length*120;
                    $scope.tbConf.tableCol = tableCol;
                })
            }

            // 获取代理商满意度列表
            function getDataList(curPage) {
                var reqUrl = "/report/agentInventoryAmountList";
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                //遍历搜索条件对象里的空值并删除  不然搜索不出数据
                Object.keys($scope.condition).forEach(function (key) {
                    if (!$scope.condition[key] && $scope.condition[key] !== 0) delete $scope.condition[key];
                });
                var param = {
                    pageNum: $scope.pageInfo.curPage,
                    pageSize: $scope.pageInfo.pageSize,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post('device', reqUrl, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.totalRow = res.value.list.splice(0, 1);
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                });
            }

            // 按钮权限code
            var btnPermissionCode = {
                agentProductCateExport: window.ActionCode.agentProductCateExport
            };

            // 按钮权限
            $scope.btnPermission = {
                agentProductCateExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
                getTableHead();
            })();
        }])
    ;
})(window, window.angular);